package com.ruoyi.ahs.controller;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ahs.service.IRushTaskDtoService;
import com.ruoyi.ahs.util.AhsUtils;
import com.ruoyi.ahs.util.RushTaskDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RushTask;
import com.ruoyi.system.service.IRushTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 抢购任务Controller
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/system/ahstask")
public class RushTaskController extends BaseController {
    @Autowired
    private IRushTaskService rushTaskService;

    @Autowired
    private IRushTaskDtoService rushTaskDtoService;

    @PostConstruct
    public void init() {
        RushTask rt = new RushTask();
        rt.setStatus("0");
        List<RushTask> rushTasks = rushTaskService.selectRushTaskList(rt);
        for (RushTask rushTask : rushTasks) {
            RushTaskDto rushTaskDto = new RushTaskDto(rushTask);
            rushTaskDtoService.addTask(rushTaskDto);
        }
        logger.info("任务初始化创建完成！");
    }

    /**
     * 查询抢购任务列表
     */
    //@PreAuthorize("@ss.hasPermi('system:ahstask:list')")
    @GetMapping("/list")
    public TableDataInfo list(RushTask rushTask) {
        startPage();
        List<RushTask> list = rushTaskService.selectRushTaskList(rushTask);
        return getDataTable(list);
    }

    /**
     * 导出抢购任务列表
     */
    //@PreAuthorize("@ss.hasPermi('system:ahstask:export')")
    @Log(title = "抢购任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RushTask rushTask) {
        List<RushTask> list = rushTaskService.selectRushTaskList(rushTask);
        ExcelUtil<RushTask> util = new ExcelUtil<RushTask>(RushTask.class);
        util.exportExcel(response, list, "抢购任务数据");
    }

    /**
     * 获取抢购任务详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:ahstask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        RushTask rushTask = rushTaskService.selectRushTaskById(id);
        return success(new RushTaskDto(rushTask));
    }

    /**
     * 新增抢购任务
     */
    //@PreAuthorize("@ss.hasPermi('system:ahstask:add')")
    @Log(title = "抢购任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RushTaskDto dto) {
        int res = rushTaskService.insertRushTask(dto.toRushTask());
        RushTask rushTask = rushTaskService.getLast();
        dto.setId(rushTask.getId());
        rushTaskDtoService.addTask(dto);
        return toAjax(res);
    }

    /**
     * 修改抢购任务
     */
    //@PreAuthorize("@ss.hasPermi('system:ahstask:edit')")
    @Log(title = "抢购任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RushTaskDto dto) {
        rushTaskDtoService.removeTask(dto.getId());
        if ("0".equals(dto.getStatus())) {
            rushTaskDtoService.addTask(dto);
        }
        return toAjax(rushTaskService.updateRushTask(dto.toRushTask()));
    }

    /**
     * 删除抢购任务
     */
    //@PreAuthorize("@ss.hasPermi('system:ahstask:remove')")
    @Log(title = "抢购任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            rushTaskDtoService.removeTask(id);
        }
        return toAjax(rushTaskService.deleteRushTaskByIds(ids));
    }

    @GetMapping("getCategoryBrand")
    public AjaxResult getCategoryBrand(@RequestParam(required = false) Long gaeaCategoryId) {
        return success(AhsUtils.getCategoryBrand(gaeaCategoryId));
    }

    @GetMapping("getProduct")
    public AjaxResult getProduct(Long gaeaCategoryId, Long gaeaBrandId) {
        return success(AhsUtils.getProduct(gaeaCategoryId, gaeaBrandId));
    }

    @GetMapping("getPpvAssociation")
    public AjaxResult getPpvAssociation(Long gaeaCategoryId, Long gaeaBrandId, Long gaeaProductId) {
        return success(AhsUtils.getPpvAssociation(gaeaCategoryId, gaeaBrandId, gaeaProductId));
    }

}
